package Collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 *
 */
public class ListDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        System.out.println(list);

        //获取给定下标位置上对应的元素
        String e =list.get(2);//array[2]
        System.out.println(e);
        //List集合可以通过遍历下标达到遍历集合元素的目的
        for (int i=0;i<list.size();i++){
            e = list.get(i);
            System.out.println(e);
        }
        /*
            E set(int index,E e)
            将给定元素设置到指定位置上,返回值为被替换的元素
         */
        String old = list.set(2,"six");
        System.out.println(list);
        System.out.println("被替换的元素:"+old);
        // 反转List
        //方式一
        for (int i=0;i<list.size()/2;i++){
            String s =list.get(i);
            String d =list.get(list.size()-1-i);
            list.set(i,d);
            list.set(list.size()-1-i,s);
        }
        System.out.println(list);

        //方式二
        List invertedList = new ArrayList();
        for (int i = list.size() - 1; i >= 0; i--) {
            invertedList.add(list.get(i));
        }
        System.out.println("反转后的List: " + invertedList);

        //方式三
        Collections.reverse(list);
        System.out.println(list);
    }
}
